package cn.chenfei.sshd.client.common;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class JarFileUtils {


    /**
     * 获取文件夹下包含mainClass的jar文件
     *
     * @param path : 路径
     * @return
     */
    public List<File> mainClassJars(String path) {
        if (!FileUtil.exist(path)) {
            return new ArrayList<>();
        }
        if (FileUtil.isDirectory(path)) { // 文件夹
            return FileUtil.loopFiles(path, file -> {
                String name = file.getName();
                if (StrUtil.endWith(name, ".jar")) { // 是一个jar包
                    try (JarFile jarFile = new JarFile(file.getPath())) {
                        Manifest manifest = jarFile.getManifest();
                        Attributes attributes = manifest.getMainAttributes();
                        String mainClass = attributes.getValue("Main-Class"); // 查找Main-Class
                        return StrUtil.isNotBlank(mainClass);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return false;
                    }
                }
                return false;
            });
        }
        if (FileUtil.isFile(path) && StrUtil.endWith(path, ".jar")) {
            return ListUtil.of(new File(path));
        }
        return new ArrayList<>();
    }

    public static void main(String[] args) throws IOException {
        TimeInterval timer = DateUtil.timer();
        JarFile jarFile = new JarFile("D:\\idea_project\\git\\springboot-root\\springboot-redis\\target\\springboot-redis-2022.10.jar");
        Manifest manifest = jarFile.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        String mainClass = attributes.getValue("Main-Class");
        jarFile.stream()
                .filter(jarEntry -> "META-INF/MANIFEST.MF".equals(jarEntry.getName())).findFirst()
                .ifPresent(jarEntry -> {
                    try {
                        InputStream inputStream = jarFile.getInputStream(jarEntry);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }

                });
        // 寻找当前目录下的所有jar文件,并且包含Main-Class
        List<File> files = FileUtil.loopFiles("D:\\idea_project\\git\\springboot-root", new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                Console.log("pathname:{}", pathname);
                String name = pathname.getName();
                if (StrUtil.endWith(name, ".jar")) {
                    try (JarFile jf = new JarFile(pathname.getPath())) {
                        Manifest manifest = jf.getManifest();
                        Attributes attributes = manifest.getMainAttributes();
                        String mainClass = attributes.getValue("Main-Class");
                        return StrUtil.isNotBlank(mainClass);
                    } catch (IOException e) {
                        return false;
                    }
                }
                return false;
            }
        });
        Console.log("jar files : {}", JSONUtil.toJsonStr(files));
        Console.log("timer : {}", timer.intervalMs());
    }

}
